package cn.acewill.yxd.yunxundian.ui.follow_up.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.ui.follow_up.FollowUpItemActivity;
import cn.acewill.yxd.yunxundian.ui.follow_up.nodeStruct.FU_row;

/**
 * Created by user_001 on 2017/12/20.
 */

public class FollowUpRowAdapter extends RecyclerView.Adapter<FollowUpRowAdapter.MyViewHolder>{
    private final LayoutInflater mLayoutInflater;
    private final Context mContext;
    private List<FU_row> mDataList;
    private int greenColor, orangeColor;

    public FollowUpRowAdapter(Context context, List<FU_row> dataList) {
        mDataList = dataList;
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        greenColor = context.getResources().getColor(R.color.green_text);
        orangeColor = context.getResources().getColor(R.color.a1_orange_color);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new MyViewHolder(mLayoutInflater.inflate(R.layout.card_follow_up_row, parent, false));
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final int adapterPosition = position;
        holder.rowName.setText(mDataList.get(position).rowName);
        holder.shopMan.setText(new StringBuilder("执行人：" + mDataList.get(position).shopMan));
        holder.ocDesc.setText(new StringBuilder("督导描述：" + mDataList.get(position).ocDesc));
        holder.wentifenxi.setText("问题分析");
        holder.xingdongjihua.setText("行动计划");
        holder.miaoshu.setText("整改结果");

        int rowStates = mDataList.get(position).rowStates;
        int itemStates = (int)Math.floor(rowStates/100);
        int planStates = (int)Math.floor(rowStates%100/10);
        int resultStates = rowStates%10;

        if(itemStates==1)
            holder.wentifenxi.setTextColor(Color.RED);
        else if(itemStates>=2)
            holder.wentifenxi.setTextColor(greenColor);

        if(planStates==1)
            holder.xingdongjihua.setTextColor(Color.RED);
        else if(planStates>=2)
            holder.xingdongjihua.setTextColor(greenColor);

        if(resultStates<2){
            if(mDataList.get(position).unULCount>0)
                holder.miaoshu.setTextColor(orangeColor);
            else if(resultStates==1)
                holder.miaoshu.setTextColor(Color.RED);
        } else {
            holder.miaoshu.setTextColor(greenColor);
        }

        holder.wentifenxi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                jumpToFUI(adapterPosition, 0);
            }
        });
        holder.xingdongjihua.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                jumpToFUI(adapterPosition,1);
            }
        });
        holder.miaoshu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                jumpToFUI(adapterPosition,2);
            }
        });
    }

    private void jumpToFUI(int adapterPosition, int tabIndex){
        Intent intent = new Intent(mContext, FollowUpItemActivity.class);
        intent.putExtra("node", mDataList.get(adapterPosition));
        intent.putExtra("tabIndex", tabIndex);
        mContext.startActivity(intent);
    }

    @Override
    public int getItemCount() {
        return mDataList == null ? 0 : mDataList.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        //@ViewInject(R.id.fuCard)
        //private CardView fuCard;
        @ViewInject(R.id.rowName)
        private TextView rowName;
        @ViewInject(R.id.shopMan)
        private TextView shopMan;
        @ViewInject(R.id.ocDesc)
        private TextView ocDesc;
        @ViewInject(R.id.wentifenxi)
        private TextView wentifenxi;
        @ViewInject(R.id.xingdongjihua)
        private TextView xingdongjihua;
        @ViewInject(R.id.miaoshu)
        private TextView miaoshu;

        MyViewHolder(View view){
            super(view);
            x.view().inject(this, view);
        }
    }
}
